package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


/**
 * @author dengchujun
 * @Description
 * @date 2024/6/13
 */
@Configuration
public class MyRabbitMassageConfig {


    @Bean
    public Queue queue(){
        /**
         * queue 队列名称
         * durable 队列是否持久化，false:队列在内存中,服务器挂掉后,队列就没了；true:服务器重启后,队列将会重新生成.注意:只是队列持久化,不代表队列中的消息持久化
         * exclusive 队列是否专属,专属的范围针对的是连接,也就是说,一个连接下面的多个信道是可见的.对于其他连接是不可见的.连接断开后,该队列会被删除.注意,不是信道断开,是连接断开.并且,就算设置成了持久化,也会删除
         * autoDelete 如果所有消费者都断开连接了,是否自动删除.如果还没有消费者从该队列获取过消息或者监听该队列,那么该队列不会删除.只有在有消费者从该队列获取过消息后,该队列才有可能自动删除(当所有消费者都断开连接,不管消息是否获取完)
         */
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("x-dead-letter-exchange","order-event-exchange");
        hashMap.put("x-dead-letter-routing-key","order.release.order");
        hashMap.put("x-message-ttl",30000);

        Queue queue=new Queue("order.delay.queue",true,false,false,hashMap);
        return queue;
    }


    @Bean
    public Queue orderReleaseOrderQueue(){
        return new Queue("order.release.order.queue",true,false,false);
    }


    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange",true,true);
    }


    @Bean
    public Binding orderCreateOrder(){
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }


    @Bean
    public Binding orderReleaseOrder(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }



    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }






}
